from datetime import datetime

from sqlmodel import SQLModel, Field, Relationship

from models.relationships.user_project import UserProjectLink
from models.relationships.user_role import UserRoleLink
from models.role import Role
from models.project import Project


class User(SQLModel, table=True):
    id: int = Field(default=None, primary_key=True)
    username: str = Field(max_length=30, unique=True, index=True)
    nickname: str = Field(max_length=30)
    password: str = Field(max_length=100, nullable=False)
    phone_number: str = Field(max_length=20)
    email: str = Field(max_length=50)
    created_at: datetime = Field(default_factory=datetime.now)
    roles: list[Role] = Relationship(back_populates="users", link_model=UserRoleLink)
    project: list[Project] = Relationship(back_populates="members", link_model=UserProjectLink)
